package com.dream.technology.service.impl;

import com.dream.technology.components.context.FlowContext;
import com.dream.technology.components.riskControl.SymbolConstan;
import com.dream.technology.db.dao.RcFlowLogMapper;
import com.dream.technology.db.entity.*;
import com.dream.technology.db.entity.vo.VoCustomerTag;
import com.dream.technology.db.entity.vo.VoFlowAll;
import com.dream.technology.db.entity.vo.VoRuleAll;
import com.dream.technology.db.entity.vo.VoRuleCollectionAll;
import com.dream.technology.service.RcFlowLogService;
import com.dream.technology.utils.ZYStringUtils;
import com.dream.technology.utils.parse.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 描述：
 *
 * @author chen_q_i@163.com
 * 2019/1/9 : 15:13.
 * @version : 1.0
 */

@Service("rcFlowLogService")
public class RcFlowLogServiceImpl implements RcFlowLogService {

    @Autowired
    private RcFlowLogMapper flowLogMapper;

    @Override
    public int countByExample(RcFlowLogQuery example) {
        return flowLogMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(RcFlowLogQuery example) {
        return flowLogMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return flowLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RcFlowLog record) {
        return flowLogMapper.insert(record);
    }

    @Override
    public int insertSelective(RcFlowLog record) {
        return flowLogMapper.insertSelective(record);
    }

    @Override
    public List<RcFlowLog> selectByExample(RcFlowLogQuery example) {
        return flowLogMapper.selectByExample(example);
    }

    @Override
    public RcFlowLog selectByPrimaryKey(Long id) {
        return flowLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(RcFlowLog record, RcFlowLogQuery example) {
        return flowLogMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(RcFlowLog record, RcFlowLogQuery example) {
        return flowLogMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(RcFlowLog record) {
        return flowLogMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RcFlowLog record) {
        return flowLogMapper.updateByPrimaryKey(record);
    }

    /**
     * 插入 标签
     *
     * @param context
     * @param tag
     * @param b       是否拒绝
     * @return
     */
    @Override
    public int insertTag(FlowContext context, VoFlowAll voFlowAll, VoCustomerTag tag, boolean b) throws Exception {

        RcFlowLog flowLog = getRcFlowLog(context, voFlowAll);
        //标签命中只有下一步
        if (b) {
            flowLog.setValue("是");
            flowLog.setFlowStrategy(SymbolConstan.FLOW_ACTION_STOP);
            flowLog.setResult(SymbolConstan.FLOW_ACTION_STOP);
        } else {
            flowLog.setValue("是");
            flowLog.setFlowStrategy(SymbolConstan.FLOW_ACTION_PASS);
            flowLog.setResult(SymbolConstan.FLOW_ACTION_PASS);
        }
        RcCustomerTag customerTag = tag.getCustomerTag();
        flowLog.setItemName(customerTag.getTagName());
        flowLog.setItemCode(customerTag.getCode());
        flowLog.setItemId(customerTag.getId());
        flowLog.setItemType(Integer.valueOf(SymbolConstan.FLOW_ITEM_TYPE_TAG));
        flowLog.setMemo(customerTag.getDescription());
        insert(flowLog);
        return 0;
    }

    /**
     * 插入 规则
     *
     * @param context
     * @param tag
     * @param b       是否拒绝
     * @return
     */
    @Override
    public int insertRule(FlowContext context, VoFlowAll voFlowAll, VoRuleCollectionAll voRuleCollectionAll,
                          VoRuleAll ruleAll, boolean b, boolean stratyge) throws Exception {

        RcFlowLog flowLog = getRcFlowLog(context, voFlowAll);

        Set<Map.Entry<String, RcRuleItem>> entries = ruleAll.getItems().entrySet();
        String fieldCode = "";
        for (Map.Entry<String, RcRuleItem> entry : entries) {
            fieldCode = entry.getValue().getFieldCode();
        }
        fieldCode = ZYStringUtils.toLowerCaseFirstOne(fieldCode);
        Object value = context.get(fieldCode);
        String s = value == null ? "值为空" : value.toString();
        flowLog.setValue(s);
        //规则是否通过 2 异常
        int i = b ? 1 : 0, j = stratyge ? 1 : 0;
        flowLog.setResult(i);
        if (voRuleCollectionAll == null) {
            // 流程下的规则才有结果
            flowLog.setFlowStrategy(j);
        } else {
            flowLog.setCollectId(voRuleCollectionAll.getRcRuleCollection().getId());
            flowLog.setCollectName(voRuleCollectionAll.getRcRuleCollection().getCollectionName());
        }
        RcRule rcRule = ruleAll.getRcRule();
        flowLog.setItemName(rcRule.getRuleName());
        flowLog.setItemCode(rcRule.getRuleCode());
        flowLog.setItemId(rcRule.getId());
        flowLog.setItemType(Integer.valueOf(SymbolConstan.FLOW_ITEM_TYPE_RULE));
        flowLog.setMemo(rcRule.getRuleDesc());
        insert(flowLog);
        return 0;
    }

    /**
     * 插入 规则集
     *
     * @param context
     * @param coll
     * @param b       是否拒绝
     * @return
     */
    @Override
    public int insertCollect(FlowContext context, VoFlowAll voFlowAll, VoRuleCollectionAll coll, boolean b, boolean stratyge) throws Exception {
        RcFlowLog flowLog = getRcFlowLog(context, voFlowAll);
        //
        int i = b ? 1 : 0, j = stratyge ? 1 : 0;
        String s = stratyge ? "是" : "否";
        flowLog.setValue(s);
        flowLog.setResult(i);
        flowLog.setFlowStrategy(j);
        RcRuleCollection collection = coll.getRcRuleCollection();
        flowLog.setItemName(collection.getCollectionName());
        flowLog.setItemCode(collection.getCollectionCode());
        flowLog.setItemId(collection.getId());
        flowLog.setItemType(Integer.valueOf(SymbolConstan.FLOW_ITEM_TYPE_COLL));
        flowLog.setMemo(collection.getCollectionName());
        return insert(flowLog);
    }

    /**
     * 插入 异常
     * <p>
     * 这里可能需要一个异常处理机 没好的思路 时间关系 下一版本吧
     *
     * @param context
     * @param e
     * @return
     */
    @Override
    public int insertExcption(FlowContext context, VoFlowAll voFlowAll, Exception e) throws Exception {
        RcFlowLog flowLog = getRcFlowLog(context, voFlowAll);
        String memo = Arrays.toString(e.getStackTrace());
        memo=memo.substring(0,memo.length()>200?200:memo.length());
        flowLog.setMemo(memo);
        flowLog.setValue("否");
        insert(flowLog);
        // 取消订单

        return 0;
    }


    public RcFlowLog getRcFlowLog(FlowContext context, VoFlowAll voFlowAll) throws Exception {
        Map<String, Object> orderFormMap = (Map<String, Object>) context.get(FlowContext.ORDERFORM);
        Map<String, Object> customerMap = (Map<String, Object>) context.get(FlowContext.CUSTOMER);
        OrderBorrowingForm orderBorrowingForm = MapUtils.mapToBean(OrderBorrowingForm.class, orderFormMap);
        RcFlowLog flowLog = new RcFlowLog();
       /* CusCustomerVo cusCustomerVo = MapUtils.mapToBean(CusCustomerVo.class, customerMap);


        flowLog.setCusName(cusCustomerVo.getRealName());
        flowLog.setCustomerId(cusCustomerVo.getId());
        flowLog.setMobile(cusCustomerVo.getMobile());*/
        flowLog.setBorrowFormId(orderBorrowingForm.getOrderId());
        RcFlow flow = voFlowAll.getFlow();
        flowLog.setFlowId(flow.getId());
        flowLog.setFlowName(flow.getFlowName());
        return flowLog;
    }


    @Override
    public List<RcFlowLog> selectControlFlowLog(Long customerId, String name) {
        return flowLogMapper.selectControlFlowLog(customerId,name);
    }
}
